package updown.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import updown.constant.LogConstant;
import updown.controller.other.BaseController;
import updown.domain.*;
import updown.service.jar.LocalFileService;
import updown.service.jar.SshService;
import updown.service.jar.UploadJarService;
import updown.service.task.ServerHostService;
import updown.service.task.TaskInfoService;
import updown.service.updown.DownUpServiceWithRetry;
import updown.service.updown.LocalService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: com.damai.downupservice.controller </p>
 * <p>Description:yq: -  </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/11/6
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/localService")
public class LocalServiceController extends BaseController {

    @Autowired
    private LocalService localService;

    @Autowired
    private TaskInfoService taskInfoService;

    @Autowired
    private ServerHostService hostService;

    @Autowired
    private SshService sshService;
    /**
     * 添加服务
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public String add(@RequestBody ServiceInfo serviceInfo) {
        log.info("add(),serviceInfo:{}",serviceInfo);
        String msg = localService.add(serviceInfo);
        addLog("添加服务：" + serviceInfo.getServiceName(), LogConstant.MODULE_LOCAL_SERVICE);
        return msg;
    }

    /**
     * 添加服务器
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/addHost")
    public String add(@RequestBody ServerHost serverHost) {
        log.info("add(),serverHost:{}",serverHost);
        String msg = localService.addHost(serverHost);
        addLog("添加主机：" + serverHost.getName(), LogConstant.MODULE_LOCAL_HOST);
        return msg;
    }

    /**
     * 查询维护的 service 信息 ，查询本地数据库的
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/serviceServerMap")
    public String serviceServerMap(Map<String,Object> model, HttpServletRequest request) {
        log.info("log.info:serviceServerMap 请求进入");
        List<ServiceMapDto> dtos = localService.getAll();
        model.put("serviceServerMapDtoList",dtos);
        List<ServerHost> hosts = hostService.getAll();
        for (ServerHost host : hosts) {
            host.setPassword("");
        }
        List<String> configErrors = sshService.checkConfig();
        model.put("hosts",hosts);
        model.put("configErrors",configErrors);
        return "localServiceMap";
    }

    /**
     * 查询维护的 service 信息 ，查询本地数据库的 + 替换的历史信息
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/getJarsAndTasks")
    public String getJarsAndTasks(@RequestParam(value = "serviceName")String serviceName, Map<String, Object> model) {
        log.info("getTaskInfoByServiceName(),serviceName:{}",serviceName);
        List<TaskWrapperMapperBean> serviceTaskInfoMap = taskInfoService.getTaskInfoByServiceName(serviceName);
        model.put("serviceTaskInfoMap",serviceTaskInfoMap);

        List<String> jarInfo = localService.getJarInfo(serviceName);
        model.put("jarInfo",jarInfo);
        model.put("serviceName",serviceName);

        return "specifyServiceMap";
    }


    /**
     * 取消当前正在 服务替换状态
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/cancelRestartIng")
    public String cancelRestartIng(@RequestParam(value = "serviceName")String serviceName) {
        log.info("cancelRestartIng(),serviceName:{}",serviceName);
        DownUpServiceWithRetry.downDownMap.remove(serviceName);
        addLog("取消当前正在服务替换：" + serviceName, LogConstant.MODULE_RESTART);
        return "成功";
    }

    /**
     * 取消当前正在 jar包分发状态
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/cancelSendJarIng")
    public String cancelSendJarIng(@RequestParam(value = "serviceName")String serviceName) {
        log.info("cancelSendJarIng(),serviceName:{}",serviceName);
        UploadJarService.sendToSeverIng.remove(serviceName);
        addLog("取消当前正在发送Jar：" + serviceName, LogConstant.MODULE_JAR_UPLOAD);
        return "成功";
    }

    @Autowired
    private LocalFileService localFileService;

    @PostMapping("/deleteJarByLocalPath")
    @ResponseBody
    public String deleteJarByLocalPath(@RequestBody ServiceMapVo vo){
        log.info("deleteJarByLocalPath(),vo:{}",vo);
        if (StringUtils.isEmpty(vo.getLocalFilePath())){
            return "";
        }
        String res = localFileService.deleteJarByLocalPath(vo.getLocalFilePath());
        addLog("删除本地jar：" + vo.getLocalFilePath(), LogConstant.MODULE_JAR_UPLOAD);
        return res;
    }
}
